package SocketHomework;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class Server {
    private ServerSocket ss;

    public Server()
    {
        try {
            System.out.println("启动服务器中");
            ss=new ServerSocket(8088);
            System.out.println("启动服务器成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start()
    {
        try {
            System.out.println("waiting connect");
            Socket socket = ss.accept();
            System.out.println("finished connect");

            InputStream is=socket.getInputStream();
            int len;
            while ((len= is.read())!=-1)
            {
                byte[] bytes = new byte[len];
                is.read(bytes);
                String s = new String(bytes, StandardCharsets.UTF_8);
                System.out.println("服务器说："+s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        Server server = new Server();
        server.start();
    }
}
